package com.tiocloud.chat.feature.session.common.adapter.viewholder;

import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.tiocloud.chat.R;
import com.tiocloud.chat.feature.session.common.adapter.MsgAdapter;
import com.tiocloud.chat.feature.session.common.adapter.msg.TioMsg;
import com.tiocloud.chat.feature.session.common.adapter.model.TioMsgType;
import com.tiocloud.chat.feature.session.common.adapter.viewholder.base.MsgBaseViewHolder;
import com.watayouxiang.imclient.model.body.wx.msg.InnerMsgImage;
import com.tiocloud.chat.util.TioImageBrowser;
import com.watayouxiang.androidutils.widget.imageview.TioImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * author : TaoWang
 * date : 2020/3/5
 * desc :
 */
public class MsgPictureViewHolder extends MsgBaseViewHolder {

    private TioImageView msgImageView;
    private String mOriginalImgUrl;

    public MsgPictureViewHolder(MsgAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int contentResId() {
        return R.layout.tio_msg_item_picture;
    }

    @Override
    protected void inflateContent() {
        msgImageView = findViewById(R.id.msgImageView);
    }

    @Override
    protected void bindContent(BaseViewHolder holder) {
        InnerMsgImage data = (InnerMsgImage) getMessage().getContentObj();
        if (data == null) return;
        mOriginalImgUrl = data.url;
        msgImageView.load_msg_pic(data.coverurl, data.width, data.height);
    }

    @Override
    protected View.OnClickListener onContentClick() {
        return this::openWatchImageActivity;
    }

    /**
     * 消息图片预览 - 增加左右滑动预览功能
     */
    private void openWatchImageActivity(View view) {
        List<TioMsg> data = getAdapter().getData();
        int size;
        if (data == null || (size = data.size()) == 0) return;

        // 获取所有 "图片消息"
        ArrayList<TioMsg> imgMsgs = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            TioMsg msg = data.get(i);
            if (msg.getMsgType() == TioMsgType.image) {
                Object contentObj = msg.getContentObj();
                if (contentObj instanceof InnerMsgImage) {
                    imgMsgs.add(msg);
                }
            }
        }

        // 获取 "图片集合"
        // 获取当前 "图片" 所处的位置
        ArrayList<String> imgUrls = new ArrayList<>(imgMsgs.size());
        int imgUrlsIndex = 0;
        int currImgPosition = 0;
        for (int i = 0, imgMsgSize = imgMsgs.size(); i < imgMsgSize; i++) {
            TioMsg msg = imgMsgs.get(i);
            if (msg.getId() != null && msg.getId().equals(getMessage().getId())) {
                currImgPosition = imgUrlsIndex;
            }
            InnerMsgImage imgMsg = (InnerMsgImage) msg.getContentObj();
            imgUrls.add(imgUrlsIndex++, imgMsg.url);
        }

        // 显示图片
        if (mOriginalImgUrl != null) {
            TioImageBrowser.getInstance().showPic(view, currImgPosition, imgUrls.toArray(new String[]{}));
        }
    }
}
